﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Globalization;
using System.Web.UI;

namespace MTO.Framework.Web.Mvc
{
    public class GroupMultiSelectList : IEnumerable<GroupSelectListItem>
    {
        public GroupMultiSelectList(Dictionary<string,IEnumerable> items)
            : this(items, null /* selectedValues */)
        {
        }

        public GroupMultiSelectList(Dictionary<string, IEnumerable> items, IEnumerable selectedValues)
            : this(items, null /* dataValuefield */, null /* dataTextField */, selectedValues)
        {
        }

        public GroupMultiSelectList(Dictionary<string, IEnumerable> items, string dataValueField, string dataTextField)
            : this(items, dataValueField, dataTextField, null /* selectedValues */)
        {
        }

        public GroupMultiSelectList(Dictionary<string, IEnumerable> items, string dataValueField, string dataTextField, IEnumerable selectedValues)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            Items = items;
            DataValueField = dataValueField;
            DataTextField = dataTextField;
            SelectedValues = selectedValues;
        }

        public string DataTextField
        {
            get;
            private set;
        }

        public string DataValueField
        {
            get;
            private set;
        }

        public Dictionary<string, IEnumerable> Items
        {
            get;
            private set;
        }

        public IEnumerable SelectedValues
        {
            get;
            private set;
        }

        public virtual IEnumerator<GroupSelectListItem> GetEnumerator()
        {
            return GetListItems().GetEnumerator();
        }

        internal IList<GroupSelectListItem> GetListItems()
        {
            return (!String.IsNullOrEmpty(DataValueField)) ?
                GetListItemsWithValueField() :
                GetListItemsWithoutValueField();
        }

        private IList<GroupSelectListItem> GetListItemsWithValueField()
        {
            HashSet<string> selectedValues = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
            if (SelectedValues != null)
            {
                selectedValues.UnionWith(from object value in SelectedValues select Convert.ToString(value, CultureInfo.CurrentCulture));
            }

            IEnumerable<GroupSelectListItem> finalList = new List<GroupSelectListItem>();
            foreach (var item in Items)
            {
                var grp = item.Key;
                var listItems = from object it in item.Value
                                let value = Eval(it, DataValueField)
                                select new GroupSelectListItem
                                {
                                    Value = value,
                                    Group = grp,
                                    Text = Eval(it, DataTextField),
                                    Selected = selectedValues.Contains(value)
                                };
                finalList = finalList.Concat(listItems);
            }

            return finalList.ToList();
        }

        private IList<GroupSelectListItem> GetListItemsWithoutValueField()
        {
            HashSet<object> selectedValues = new HashSet<object>();
            if (SelectedValues != null)
            {
                selectedValues.UnionWith(SelectedValues.Cast<object>());
            }

            IEnumerable<GroupSelectListItem> finalList = new List<GroupSelectListItem>();
            foreach (var item in Items)
            {
                var grp = item.Key;
                var listItems = from object it in item.Value
                            select new GroupSelectListItem
                            {
                                Group = grp,
                                Text = Eval(it, DataTextField),
                                Selected = selectedValues.Contains(it)
                            };
                finalList = finalList.Concat(listItems);
            }

            return finalList.ToList();
        }

        private static string Eval(object container, string expression)
        {
            object value = container;
            if (!String.IsNullOrEmpty(expression))
            {
                value = DataBinder.Eval(container, expression);
            }
            return Convert.ToString(value, CultureInfo.CurrentCulture);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
